import axios from 'axios'

const api = axios.create({
  baseURL: '/api/v1',
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json'
  }
})

export interface User {
  id: number
  username: string
  email: string
  created_at: string
  updated_at: string
}

export interface CreateUserRequest {
  username: string
  email: string
  password: string
}

export interface UpdateUserRequest {
  username?: string
  email?: string
  password?: string
}

export const userApi = {
  // 获取用户列表
  getUsers: async () => {
    const response = await api.get<User[]>('/users')
    return response.data
  },

  // 获取单个用户
  getUser: async (id: number) => {
    const response = await api.get<User>(`/users/${id}`)
    return response.data
  },

  // 创建用户
  createUser: async (data: CreateUserRequest) => {
    const response = await api.post<User>('/users', data)
    return response.data
  },

  // 更新用户
  updateUser: async (id: number, data: {
    username?: string;
    email?: string;
    password?: string;
  }) => {
    const response = await api.put<User>(`/users/${id}`, data)
    return response.data
  },

  // 删除用户
  deleteUser: async (id: number) => {
    await api.delete(`/users/${id}`)
  }
}
